package ajax;

import beans.MainBean;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

public class MessagesAjaxBean extends MainBean {

    private JsonObject jsonRes = new JsonObject();
    
    public MessagesAjaxBean() {
        super();
    }

    public String getRunAjax() {
        choiseMethod();
        return jsonRes.toString();
    }

    public JsonObject getJsonRes() {
        return jsonRes;
    }

    public void addPropertyToRes(String property, String value) {
        jsonRes.addProperty(property, value);
    }
    
    public void addPropertyToRes(String property, JsonArray value) {
        jsonRes.addProperty(property, value.toString());
    }
    
    private void choiseMethod() {
        try {
            String act = getStringFromParam("action");
            if (act != null && !act.isEmpty()) {
                MessagesAjaxActions action = MessagesAjaxActions.getActionFromString(act);
                action.doAction(this);
                jsonRes.addProperty("status", "ok");
            }
        } catch (Exception e) {
            jsonRes.addProperty("status", "error");
        }
    }

    @Override
    public boolean isLoad() {
        return true;
    }
}
